#!/bin/bash
# Linux 内核日常自动化测试脚本（轮询模式）
# 版本: 2.1
# 描述: 执行常规测试和性能测试（轮询模式），动态获取稳定标签

# 基础配置
REPO_ROOT="/srv/git/auto_test_repos"
export RESULT_ROOT="/srv/log/kernel_ci"
export REPO_BASE_DIR="$REPO_ROOT"  # 传递给 linux_auto_test.py
mkdir -p "$RESULT_ROOT"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"  # 获取脚本所在目录
export LKP_SRC="${LKP_SRC:-$(cd "$SCRIPT_DIR/../../.." && pwd)}"  # 设置LKP_SRC路径


# 轮询间隔（秒）
POLL_INTERVAL=86400  # 24小时
RUN_COUNT=0

# 仓库配置 - 只保留 openEuler kernel OLK-5.10 和 OLK-6.6
REPOS=(
    "openeuler-kernel=git://172.168.131.113:9418/new-upstream/l/linux/openeuler-kernel.git"
    "linux-next=git://172.168.131.113:9418/new-upstream/l/linux/linux-next.git"
    "linux=git://172.168.131.113:9418/new-upstream/l/linux/linux.git"
    #"linux=https://mirrors.tuna.tsinghua.edu.cn/git/linux.git"
    #"linux-stable=https://mirrors.tuna.tsinghua.edu.cn/git/linux-stable.git"
)

# 测试配置 - 增加构建测试支持
# 格式: test_name:template_path:test_type:commit_override_path:suite_name
# test_type: functional|performance|build
# commit_override_path: 用于指定不同测试类型的commit字段路径
# suite_name: 实际的suite名称（可选，默认使用test_name）
TESTS=(
    "build_kernel:programs/bisect-py/jobs/auto_test/build_kernel.yaml:build:program.makepkg.commit:makepkg"
    "ltp:programs/bisect-py/jobs/auto_test/ltp.yaml:functional:ss.linux.commit:ltp"
    "trinity:programs/bisect-py/jobs/auto_test/trinity.yaml:functional:ss.linux.commit:trinity"
    "xfstests:programs/bisect-py/jobs/auto_test/xfstest.yaml:functional:ss.linux.commit:xfstests"
    "cpu-hotplug:programs/bisect-py/jobs/auto_test/cpu-hotplug.yaml:functional:ss.linux.commit:cpu-hotplug"
    "kernel-selftests:programs/bisect-py/jobs/auto_test/kernel-selftests.yaml:functional:ss.linux.commit:kernel-selftests"
    "unixbench:programs/bisect-py/jobs/auto_test/unixbench.yaml:performance:ss.linux.commit:unixbench"
    "lmbench:programs/bisect-py/jobs/auto_test/lmbench.yaml:performance:ss.linux.commit:lmbench"
    "fio:programs/bisect-py/jobs/auto_test/fio.yaml:performance:ss.linux.commit:fio"
)

# 构建配置列表 - 用于build类型的测试
# randconfig 使用日期后缀，每天生成新的配置，方便问题回溯
DAILY_DATE=$(date +%Y%m%d)
BUILD_CONFIGS=(
    "allmodconfig"
    "allyesconfig"
    "allnoconfig"
    "randconfig-${DAILY_DATE}-1"
    "randconfig-${DAILY_DATE}-2"
    "randconfig-${DAILY_DATE}-3"
)

# 统一的测试配置
# 格式: repo_name:branch:baseline_tag_spec:current_tag_spec:test_types
# tag_spec 格式:
#   - fixed:TAG_NAME    固定tag
#   - dynamic:PATTERN   动态获取匹配模式的最新tag
#   - 空字符串         不使用该tag
# test_types: functional|performance|build|all (all表示执行所有类型)
KERNEL_TEST_CONFIG=(
    # openEuler kernel - OLK-5.10
    "openeuler-kernel:OLK-5.10:fixed:5.10.0-216.0.0:dynamic:5.10.0-2*.0.0:all"
    # openEuler kernel - OLK-6.6
    "openeuler-kernel:OLK-6.6:fixed:6.6.0-98.0.0:dynamic:6.6.0-*.0.0:all"
    # linux-next - master
    "linux-next:master:fixed:v6.17:dynamic:next-*:all"
    "linux:master:fixed:v6.17:dynamic:v6.18-rc*:all"
)

# 函数：解析 tag spec 并获取实际 tag
# 参数: repo_dir, tag_spec
# 返回: 实际的 tag 值
resolve_tag_spec() {
    local repo_dir="$1"
    local tag_spec="$2"

    if [[ -z "$tag_spec" ]]; then
        echo ""
        return 0
    fi

    local spec_type="${tag_spec%%:*}"
    local spec_value="${tag_spec#*:}"

    case "$spec_type" in
        fixed)
            # 固定tag，直接返回
            echo "$spec_value"
            ;;
        dynamic)
            # 动态获取匹配模式的最新tag
            if [[ ! -d "$repo_dir" ]]; then
                echo ""
                return 1
            fi

            cd "$repo_dir" || return 1
            local latest_tag=$(git tag -l "$spec_value" | sort -V | tail -n 1)
            cd - > /dev/null

            if [[ -z "$latest_tag" ]]; then
                echo "警告: 无法动态获取 tag (pattern: $spec_value)" >&2
                echo ""
                return 1
            fi
            echo "$latest_tag"
            ;;
        *)
            echo "错误: 未知的 tag spec 类型: $spec_type" >&2
            echo ""
            return 1
            ;;
    esac
}

# 函数：获取统一的测试配置
# 参数: repo_name, branch, repo_dir
# 返回: baseline_tag:current_tag:test_types (如果找不到配置则返回空)
get_test_config() {
    local repo_name="$1"
    local branch="$2"
    local repo_dir="$3"

    for config_entry in "${KERNEL_TEST_CONFIG[@]}"; do
        IFS=':' read -r cfg_repo cfg_branch baseline_type baseline_val current_type current_val test_types <<< "$config_entry"

        if [[ "$cfg_repo" == "$repo_name" && "$cfg_branch" == "$branch" ]]; then
            # 解析 baseline tag
            local baseline_tag=""
            if [[ -n "$baseline_type" ]]; then
                echo "[$(date '+%T')] 正在解析 baseline tag (spec: $baseline_type:$baseline_val)..."
                baseline_tag=$(resolve_tag_spec "$repo_dir" "$baseline_type:$baseline_val")
                echo "[$(date '+%T')] 解析结果: baseline_tag = ${baseline_tag:-'未找到'}"
            fi

            # 解析 current tag
            local current_tag=""
            if [[ -n "$current_type" ]]; then
                echo "[$(date '+%T')] 正在解析 current tag (spec: $current_type:$current_val)..."
                current_tag=$(resolve_tag_spec "$repo_dir" "$current_type:$current_val")
                echo "[$(date '+%T')] 解析结果: current_tag = ${current_tag:-'未找到'}"
            fi

            # 如果 baseline_tag 为空，但 current_tag 存在，则尝试自动查找 base tag
            if [[ -z "$baseline_tag" && -n "$current_tag" ]]; then
                echo "[$(date '+%T')] Baseline tag 为空，尝试基于 current tag ($current_tag) 自动查找..."
                local base_rc_script="$LKP_SRC/lib/git-base-rc-tag.sh"
                if [[ -f "$base_rc_script" ]]; then
                    # 调用脚本并捕获输出
                    baseline_tag=$(bash "$base_rc_script" -C "$repo_dir" "$current_tag" 2>/dev/null)
                    echo "[$(date '+%T')] 自动查找结果: baseline_tag = ${baseline_tag:-'未找到'}"
                else
                    echo "[$(date '+%T')] 警告: 自动查找脚本不存在: $base_rc_script"
                fi
            fi

            echo "$baseline_tag:$current_tag:$test_types"
            return 0
        fi
    done

    echo ""
    return 1
}

# 函数：获取仓库的所有配置分支
# 参数: repo_name
# 返回: 空格分隔的分支列表
get_repo_branches() {
    local repo_name="$1"
    local branches=()

    for config_entry in "${KERNEL_TEST_CONFIG[@]}"; do
        IFS=':' read -r cfg_repo cfg_branch rest <<< "$config_entry"
        if [[ "$cfg_repo" == "$repo_name" ]]; then
            branches+=("$cfg_branch")
        fi
    done

    echo "${branches[@]}"
}

# 函数：记录提交 ID
record_commit_id() {
    local repo_dir="$1"
    local branch="$2"
    local tag="$3"
    local log_file="$4"

    if [[ -d "$repo_dir" ]]; then
        cd "$repo_dir" || return 1
        local commit_id=$(git rev-parse "$tag" 2>/dev/null || echo "unknown")
        echo "[$(date '+%T')] 分支: $branch, Tag: $tag, CommitID: $commit_id" >> "$log_file"
        cd - > /dev/null
    fi
}

# 函数：带日志记录执行命令
run_with_log() {
    local log_file="$1"
    shift
    echo "========================================" >> "$log_file"
    echo "[$(date '+%F %T')] 执行命令: $*" >> "$log_file"
    echo "----------------------------------------" >> "$log_file"
    # 执行命令并捕获输出
    "$@" >> "$log_file" 2>&1
    local status=$?
    echo "----------------------------------------" >> "$log_file"
    echo "退出状态: $status" >> "$log_file"
    echo "========================================" >> "$log_file"
    return $status
}

# 初始化全局变量
init_vars() {
    RUN_COUNT=0
    POLL_INTERVAL=${POLL_INTERVAL:-86400}  # 默认24小时
}


# 主执行流程 - 轮询模式
main() {
    # 确保在主函数开始时初始化变量
    init_vars || {
        echo "初始化失败"
        return 1
    }
    
    while true; do
        RUN_COUNT=$((RUN_COUNT+1))
        CURRENT_TS=$(date +%s)
        TODAY=$(date +%Y%m%d)
        LOG_DIR="$RESULT_ROOT/$TODAY"
        mkdir -p "$LOG_DIR" "$LOG_DIR/results"
        
        # 初始化日常摘要文件
        DAILY_SUMMARY="$LOG_DIR/daily_summary.txt"
        echo "======================================" > "$DAILY_SUMMARY"
        echo "Linux 内核日常测试摘要 - $TODAY" >> "$DAILY_SUMMARY"
        echo "开始时间: $(date '+%F %T')" >> "$DAILY_SUMMARY"
        echo "======================================" >> "$DAILY_SUMMARY"
        
        echo "========================================"
        echo "[$(date '+%F %T')] 开始轮询执行 #$RUN_COUNT"
        echo "========================================"

        # 1. 清理旧日志（保留最近30天）
        find "$RESULT_ROOT" -mindepth 1 -maxdepth 1 -type d -mtime +30 -exec rm -rf {} \;
    
        # 2. 以仓库为外层循环
        for repo_entry in "${REPOS[@]}"; do
            IFS='=' read -r repo_name repo_url <<< "$repo_entry"
            repo_dir="$REPO_ROOT/$repo_name"

        # 3. 统一的测试执行逻辑
        echo "[$(date '+%F %T')] === 开始 $repo_name 测试 ===" | tee -a "$LOG_DIR/test.log"

        # 克隆或更新仓库
        if [[ ! -d "$repo_dir/.git" ]]; then
            echo "[$(date '+%F %T')] 克隆仓库: $repo_url -> $repo_dir" | tee -a "$LOG_DIR/test.log"
            git clone "$repo_url" "$repo_dir" >> "$LOG_DIR/test.log" 2>&1
            if [[ $? -ne 0 ]]; then
                echo "错误: 克隆仓库失败: $repo_url" | tee -a "$LOG_DIR/test.log"
                continue
            fi
        else
            echo "[$(date '+%F %T')] 更新仓库: $repo_dir" | tee -a "$LOG_DIR/test.log"
            cd "$repo_dir" || continue
            git fetch --all --tags >> "$LOG_DIR/test.log" 2>&1
            if [[ $? -ne 0 ]]; then
                echo "警告: 更新仓库失败: $repo_dir" | tee -a "$LOG_DIR/test.log"
            fi
            cd - > /dev/null
        fi

        # 获取仓库的所有配置分支
        branches=$(get_repo_branches "$repo_name")

        if [[ -z "$branches" ]]; then
            echo "跳过 $repo_name: 没有找到测试配置" | tee -a "$LOG_DIR/test.log"
            continue
        fi

        # 遍历每个分支
        for branch in $branches; do
            echo "[$(date '+%F %T')] 处理分支: $repo_name/$branch" | tee -a "$LOG_DIR/test.log"

            # 获取分支的测试配置
            echo "[$(date '+%F %T')] 调用 get_test_config for $repo_name/$branch" | tee -a "$LOG_DIR/test.log"
            test_config=$(get_test_config "$repo_name" "$branch" "$repo_dir")
            echo "[$(date '+%F %T')] get_test_config 返回: '$test_config'" | tee -a "$LOG_DIR/test.log"

            if [[ -z "$test_config" ]]; then
                echo "警告: 未找到分支 $repo_name/$branch 的配置" | tee -a "$LOG_DIR/test.log"
                continue
            fi

            # 提取最后一行（实际的配置数据），过滤掉调试输出
            test_config_data=$(echo "$test_config" | tail -n 1)
            echo "[$(date '+%F %T')] 解析配置数据: '$test_config_data'" | tee -a "$LOG_DIR/test.log"

            IFS=':' read -r baseline_tag current_tag test_types <<< "$test_config_data"
            echo "[$(date '+%F %T')] 解析结果: baseline_tag='$baseline_tag', current_tag='$current_tag', test_types='$test_types'" | tee -a "$LOG_DIR/test.log"

            # 记录提交ID
            [[ -n "$baseline_tag" ]] && record_commit_id "$repo_dir" "$branch" "$baseline_tag" "$LOG_DIR/commit_ids.log"
            [[ -n "$current_tag" ]] && record_commit_id "$repo_dir" "$branch" "$current_tag" "$LOG_DIR/commit_ids.log"

            # 功能测试
            if [[ "$test_types" == "functional" || "$test_types" == "all" ]]; then
                echo "[$(date '+%F %T')] 开始功能测试: $repo_name/$branch" | tee -a "$LOG_DIR/functional.log"

                for test_entry in "${TESTS[@]}"; do
                    IFS=':' read -r test_name template_path test_type commit_path suite_name <<< "$test_entry"

                    # 只执行功能测试类型
                    if [[ "$test_type" != "functional" ]]; then
                        continue
                    fi

                    RESULT_PATH="$LOG_DIR/results/$repo_name-$branch-$test_name-functional"

                    # 使用 current_tag 和正确的 commit override path 进行功能测试
                    # 传递 --repo-dir 避免重复克隆
                    job_output=$(python3 $LKP_SRC/programs/bisect-py/kernel-ci/linux_auto_test.py \
                        --repo "$repo_url" \
                        --repo-dir "$repo_dir" \
                        --template "$LKP_SRC/$template_path" \
                        --test-type "$test_type" \
                        --override "$commit_path=$current_tag" \
                        --result-root "$RESULT_PATH" 2>&1)

                    # 记录完整输出到日志
                    echo "========================================" >> "$LOG_DIR/functional.log"
                    echo "[$(date '+%F %T')] 执行命令: python3 linux_auto_test.py ..." >> "$LOG_DIR/functional.log"
                    echo "$job_output" >> "$LOG_DIR/functional.log"
                    echo "========================================" >> "$LOG_DIR/functional.log"

                    # 提取job_id
                    job_id=$(echo "$job_output" | grep -o "JobID=[^[:space:]]*" | cut -d'=' -f2)

                    # 记录到日常摘要
                    if [[ -n "$job_id" ]]; then
                        echo "[$(date '+%T')] 功能测试: $repo_name/$branch/$test_name (tag: $current_tag, job_id: $job_id)" >> "$DAILY_SUMMARY"
                    else
                        echo "[$(date '+%T')] 功能测试: $repo_name/$branch/$test_name (tag: $current_tag, job_id: 获取失败)" >> "$DAILY_SUMMARY"
                    fi
                done
            fi

            # 性能测试
            if [[ "$test_types" == "performance" || "$test_types" == "all" ]]; then
                # 性能测试需要 baseline 和 current 两个 tag
                if [[ -z "$baseline_tag" || -z "$current_tag" ]]; then
                    echo "跳过性能测试: $repo_name/$branch (缺少 baseline 或 current tag)" | tee -a "$LOG_DIR/performance.log"
                    continue
                fi

                echo "[$(date '+%F %T')] 开始性能测试: $repo_name/$branch" | tee -a "$LOG_DIR/performance.log"

                for test_entry in "${TESTS[@]}"; do
                    IFS=':' read -r test_name template_path test_type commit_path suite_name <<< "$test_entry"

                    # 只执行性能测试
                    if [[ "$test_type" != "performance" ]]; then
                        continue
                    fi

                    # 提交第一个任务：baseline版本
                    PERF_PATH_BASELINE="$LOG_DIR/results/$repo_name-$branch-$test_name-performance-baseline"
                    baseline_output=$(python3 $LKP_SRC/programs/bisect-py/kernel-ci/linux_auto_test.py \
                        --repo "$repo_url" \
                        --repo-dir "$repo_dir" \
                        --template "$LKP_SRC/$template_path" \
                        --test-type "$test_type" \
                        --override "$commit_path=$baseline_tag" \
                        --result-root "$PERF_PATH_BASELINE" 2>&1)

                    # 记录baseline任务输出到日志
                    echo "========================================" >> "$LOG_DIR/performance.log"
                    echo "[$(date '+%F %T')] 执行命令: python3 linux_auto_test.py (baseline) ..." >> "$LOG_DIR/performance.log"
                    echo "$baseline_output" >> "$LOG_DIR/performance.log"
                    echo "========================================" >> "$LOG_DIR/performance.log"

                    # 提取baseline job_id
                    baseline_job_id=$(echo "$baseline_output" | grep -o "JobID=[^[:space:]]*" | cut -d'=' -f2)

                    # 提交第二个任务：current版本
                    PERF_PATH_CURRENT="$LOG_DIR/results/$repo_name-$branch-$test_name-performance-current"
                    current_output=$(python3 $LKP_SRC/programs/bisect-py/kernel-ci/linux_auto_test.py \
                        --repo "$repo_url" \
                        --repo-dir "$repo_dir" \
                        --template "$LKP_SRC/$template_path" \
                        --test-type "$test_type" \
                        --override "$commit_path=$current_tag" \
                        --result-root "$PERF_PATH_CURRENT" 2>&1)

                    # 记录current任务输出到日志
                    echo "========================================" >> "$LOG_DIR/performance.log"
                    echo "[$(date '+%F %T')] 执行命令: python3 linux_auto_test.py (current) ..." >> "$LOG_DIR/performance.log"
                    echo "$current_output" >> "$LOG_DIR/performance.log"
                    echo "========================================" >> "$LOG_DIR/performance.log"

                    # 提取current job_id
                    current_job_id=$(echo "$current_output" | grep -o "JobID=[^[:space:]]*" | cut -d'=' -f2)

                    # 记录到日常摘要
                    baseline_id_info="${baseline_job_id:-获取失败}"
                    current_id_info="${current_job_id:-获取失败}"
                    echo "[$(date '+%T')] 性能测试: $repo_name/$branch/$test_name (baseline: $baseline_tag job_id: $baseline_id_info, current: $current_tag job_id: $current_id_info)" >> "$DAILY_SUMMARY"
                done
            fi

            # 构建测试
            if [[ "$test_types" == "build" || "$test_types" == "all" ]]; then
                echo "[$(date '+%F %T')] 开始构建测试: $repo_name/$branch" | tee -a "$LOG_DIR/build.log"

                for test_entry in "${TESTS[@]}"; do
                    IFS=':' read -r test_name template_path test_type commit_path suite_name <<< "$test_entry"

                    # 只执行构建测试
                    if [[ "$test_type" != "build" ]]; then
                        continue
                    fi

                    # 遍历每个构建配置
                    for build_config in "${BUILD_CONFIGS[@]}"; do
                        RESULT_PATH="$LOG_DIR/results/$repo_name-$branch-$test_name-$build_config"

                        # 构建测试使用 current_tag 和正确的 commit override path
                        # 使用 autoconfig 而非 config，允许配置自动生成和上传
                        # 传递 --repo-dir 避免重复克隆
                        build_output=$(python3 $LKP_SRC/programs/bisect-py/kernel-ci/linux_auto_test.py \
                            --repo "$repo_url" \
                            --repo-dir "$repo_dir" \
                            --template "$LKP_SRC/$template_path" \
                            --test-type "functional" \
                            --override "$commit_path=$current_tag" \
                            --override "program.makepkg.autoconfig=$build_config" \
                            --result-root "$RESULT_PATH" 2>&1)

                        # 记录完整输出到日志
                        echo "========================================" >> "$LOG_DIR/build.log"
                        echo "[$(date '+%F %T')] 执行命令: python3 linux_auto_test.py (config: $build_config) ..." >> "$LOG_DIR/build.log"
                        echo "$build_output" >> "$LOG_DIR/build.log"
                        echo "========================================" >> "$LOG_DIR/build.log"

                        # 提取job_id
                        job_id=$(echo "$build_output" | grep -o "JobID=[^[:space:]]*" | cut -d'=' -f2)

                        # 记录到日常摘要
                        if [[ -n "$job_id" ]]; then
                            echo "[$(date '+%T')] 构建测试: $repo_name/$branch/$build_config (tag: $current_tag, job_id: $job_id)" >> "$DAILY_SUMMARY"
                        else
                            echo "[$(date '+%T')] 构建测试: $repo_name/$branch/$build_config (tag: $current_tag, job_id: 获取失败)" >> "$DAILY_SUMMARY"
                        fi
                    done
                done
            fi
        done
        done  # 结束仓库循环

        # 计算下次执行时间
        NEXT_RUN_TS=$((CURRENT_TS + POLL_INTERVAL))
        NEXT_RUN_STR=$(date -d "@$NEXT_RUN_TS" "+%Y-%m-%d %H:%M:%S")
        
        # 查询并总结过去24小时的任务执行情况
        echo "" >> "$DAILY_SUMMARY"
        echo "=== 过去24小时任务执行总结 ===" >> "$DAILY_SUMMARY"
        
        # 为所有测试套件查询任务状态（functional、build、performance）
        for test_entry in "${TESTS[@]}"; do
            IFS=':' read -r test_name template_path test_type commit_path suite_name <<< "$test_entry"

            # 如果没有指定 suite_name，使用 test_name
            suite_name="${suite_name:-$test_name}"

            echo "" >> "$DAILY_SUMMARY"
            echo "--- $test_name 测试套件总结 (类型: $test_type, suite: $suite_name) ---" >> "$DAILY_SUMMARY"

            # 查询成功任务（显示errid获取告警信息）
            echo "[成功任务]" >> "$DAILY_SUMMARY"
            python3 $LKP_SRC/programs/bisect-py/utils/query_auto_test_jobs.py \
                --hours 24 \
                --suite "$suite_name" \
                --job-health success \
                --show-jobs \
                --show-errid >> "$DAILY_SUMMARY" 2>&1

            echo "" >> "$DAILY_SUMMARY"
            echo "[失败任务]" >> "$DAILY_SUMMARY"
            python3 $LKP_SRC/programs/bisect-py/utils/query_auto_test_jobs.py \
                --hours 24 \
                --suite "$suite_name" \
                --job-health fail \
                --show-jobs \
                --show-errid >> "$DAILY_SUMMARY" 2>&1

            echo "" >> "$DAILY_SUMMARY"
            echo "[中止任务]" >> "$DAILY_SUMMARY"
            python3 $LKP_SRC/programs/bisect-py/utils/query_auto_test_jobs.py \
                --hours 24 \
                --suite "$suite_name" \
                --job-health abort \
                --show-jobs \
                --show-errid >> "$DAILY_SUMMARY" 2>&1
        done
        
        # 完成日常摘要
        echo "" >> "$DAILY_SUMMARY"
        echo "完成时间: $(date '+%F %T')" >> "$DAILY_SUMMARY"
        echo "下次执行: $NEXT_RUN_STR" >> "$DAILY_SUMMARY"
        echo "======================================" >> "$DAILY_SUMMARY"
        
        # 统计结果目录
        RESULT_COUNT=$(find "$LOG_DIR/results" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | wc -l)
        echo "本次测试生成结果目录: $RESULT_COUNT 个" >> "$DAILY_SUMMARY"
        
        # 拷贝结果到 /tmp/linux_auto_test_result/
        BACKUP_ROOT="/tmp/linux_auto_test_result"
        mkdir -p "$BACKUP_ROOT"
        echo "" >> "$DAILY_SUMMARY"
        echo "正在备份结果到 $BACKUP_ROOT ..." >> "$DAILY_SUMMARY"
        
        # 拷贝当天的整个日志目录
        if cp -r "$LOG_DIR" "$BACKUP_ROOT/" 2>/dev/null; then
            echo "结果备份成功: $BACKUP_ROOT/$TODAY" >> "$DAILY_SUMMARY"
            echo "[$(date '+%F %T')] 结果已备份到: $BACKUP_ROOT/$TODAY"
        else
            echo "结果备份失败，请检查权限" >> "$DAILY_SUMMARY"
            echo "[$(date '+%F %T')] 警告: 结果备份失败: $BACKUP_ROOT/$TODAY"
        fi
        
        echo "========================================"
        echo "[$(date '+%F %T')] 轮询执行完成"
        echo "下次执行时间: $NEXT_RUN_STR"
        echo "日常摘要已保存至: $DAILY_SUMMARY"
        echo "========================================"
        
        # 睡眠直到下次执行
        sleep $POLL_INTERVAL
    done
}

# 测试函数 - 测试查询功能
test_query_all_suites() {
    echo "========================================"
    echo "测试：查询所有测试套件的 errid"
    echo "时间: $(date '+%F %T')"
    echo "========================================"

    local test_log="/tmp/daily_kernel_test_query_test_$(date +%Y%m%d_%H%M%S).log"

    echo "测试日志将保存到: $test_log"
    echo ""

    # 测试每个测试套件
    for test_entry in "${TESTS[@]}"; do
        IFS=':' read -r test_name template_path test_type commit_path suite_name <<< "$test_entry"

        # 如果没有指定 suite_name，使用 test_name
        suite_name="${suite_name:-$test_name}"

        echo "========================================" | tee -a "$test_log"
        echo "测试套件: $test_name (类型: $test_type, suite: $suite_name)" | tee -a "$test_log"
        echo "========================================" | tee -a "$test_log"

        # 查询最近24小时的任务
        echo "" | tee -a "$test_log"
        echo "查询最近24小时的任务..." | tee -a "$test_log"
        python3 "$LKP_SRC/programs/bisect-py/utils/query_auto_test_jobs.py" \
            --hours 24 \
            --suite "$suite_name" \
            --show-jobs \
            --show-errid 2>&1 | tee -a "$test_log"

        echo "" | tee -a "$test_log"
        echo "查询完成，退出码: ${PIPESTATUS[0]}" | tee -a "$test_log"
        echo "" | tee -a "$test_log"
    done

    echo "========================================"
    echo "测试完成"
    echo "详细日志: $test_log"
    echo "========================================"
    echo ""
    echo "检查日志中是否有 errid 输出："
    echo "---"
    grep -i "errid" "$test_log" | head -20 || echo "未找到 errid 输出"
    echo "---"
}

# 确保脚本不是被源引用的
if [ "${BASH_SOURCE[0]}" -ef "$0" ]; then
    # 检查是否传入 --test 参数
    if [[ "$1" == "--test" ]]; then
        # 执行测试函数
        test_query_all_suites
    else
        # 执行主函数
        main "$@"
    fi
fi
